import 'dart:async';
import 'dart:html' as html;

import 'dart:typed_data';

class WFFile {
	// static Tuple2<String,String> splitFilename(String fileName){
	// 	List<String> splitedFileName = fileName.split(".");
	// 	return Tuple2(splitedFileName[0], splitedFileName[1]);
	// }

  static Future<Uint8List> obtainPhotoUint8List() {
    final completer = new Completer<Uint8List>();
    final html.InputElement input = html.FileUploadInputElement() as html.InputElement;
    input
      ..type = 'file'
      ..accept = 'image/*';
    input.onChange.listen((e) async {
      final List<html.File>? files = input.files;
      final reader = html.FileReader();
      reader.readAsArrayBuffer(files![0]);
      reader.onError.listen((error) => completer.completeError(error));
      await reader.onLoad.first;
      var bs = (reader.result as Uint8List); // using CryptoUtils from Dart SDK
      completer.complete(bs);
    });
    input.click();
    return completer.future;
  }

  Future<String> obtainPhotoBase64() {
    final completer = Completer<String>();
    final html.InputElement input = html.FileUploadInputElement() as html.InputElement;
    input
      ..type = 'file'
      ..accept = 'image/*';
    input.onChange.listen((e) async {
      final List<html.File>? files = input.files;
      final reader = html.FileReader();
      reader.readAsDataUrl(files![0]);
      reader.onError.listen((error) => completer.completeError(error));
      await reader.onLoad.first;
      String s= reader.result as String;
      completer.complete(s);
    });
    input.click();
    return completer.future;
  }

}